/*
Simple Forum Auto Poster.
Copyright (C) 2011 Gianni Van Hoecke.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package domain.cooldown;

import domain.forumdata.Forum;
import domain.threading.CoolDownTicker;
import java.util.ArrayList;

/**
 *
 * @author Gianni Van Hoecke <gianni.vh@gmail.com>
 */
public class CoolDownQueue
{
    private ArrayList<CoolDownEntry> coolDownQueue;
    private boolean isTickerRunning;
    private CoolDownTicker coolDownTicker;

    /**
     * Default constructor. Initializes the queue.
     */
    public CoolDownQueue()
    {
        this.coolDownQueue = new ArrayList<CoolDownEntry>();
        this.isTickerRunning = false;
    }

    /**
     * Adds a forum to the queue, and starts the ticker if not already running.
     * @param forum The forum to add.
     */
    public void addForumToQueue(Forum forum)
    {
        this.coolDownQueue.add(new CoolDownEntry(forum));
        this.startTickerIfNeeded();
    }

    /**
     * Starts the ticker if it's not running already.
     */
    private void startTickerIfNeeded()
    {
        if(!this.isTickerRunning)
        {
            this.coolDownTicker = new CoolDownTicker();
            this.coolDownTicker.start();
            this.isTickerRunning = true;
        }
    }

    /**
     * Checks whether a forum is in the queue or not.
     * @param forum The forum to lookup.
     * @return True if the forum is marked to cool down, false otherwise.
     */
    public boolean isForumInQueue(Forum forum)
    {
        for(CoolDownEntry cde : this.coolDownQueue)
        {
            if(cde.getForum().equals(forum))
                return true;
        }
        
        return false;
    }

    /**
     * @return Whether the ticker is running or not.
     */
    public boolean isIsTickerRunning()
    {
        return isTickerRunning;
    }

    /**
     * Sets the ticker running flag.
     * <strong>This method should only be called from within the ticker thread.</strong>
     * @param isTickerRunning The flag value.
     */
    public void setIsTickerRunning(boolean isTickerRunning)
    {
        this.isTickerRunning = isTickerRunning;
    }

    /**
     * @return The current cool down queue.
     */
    public ArrayList<CoolDownEntry> getCoolDownQueue()
    {
        return coolDownQueue;
    }
}